﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;

namespace Noris.Tools.TraceVisualiser.Components
{
    internal class DPicturePanel : DblGraphPanel
    {
        public DPicturePanel()
        {
        }
        /// <summary>
        /// Soupis obrázků
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<DPictureItem> PictureList { get { return this._PictureList; } }
        private List<DPictureItem> _PictureList = new List<DPictureItem>();
        /// <summary>
        /// Událost volaná před každým kreslením do bufferu
        /// </summary>
        public event EventHandler PaintPictureToBuffer;
        protected virtual void OnPaintPictureToBuffer()
        {
            if (this.PaintPictureToBuffer != null)
                this.PaintPictureToBuffer(this, EventArgs.Empty);
        }
        protected override void OnPaintToBuffer(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            this.OnPaintPictureToBuffer();
            base.OnPaintToBuffer(sender, e);
            if (this.PictureList != null)
            {
                foreach (DPictureItem picture in this.PictureList)
                {
                    if (picture.Image != null && picture.Visible &&
                        picture.Bounds.Width > 0 && picture.Bounds.Height > 0 &&
                        this.ClientRectangle.IntersectsWith(picture.Bounds))
                    {
                        switch (picture.DrawMode)
                        {
                            case DPictureDrawMode.Default:
                                e.Graphics.DrawImage(picture.Image, picture.Bounds);
                                break;
                            case DPictureDrawMode.Unscaled:
                                e.Graphics.DrawImageUnscaled(picture.Image, picture.Bounds);
                                break;
                            case DPictureDrawMode.UnscaledAndClipped:
                                e.Graphics.DrawImageUnscaledAndClipped(picture.Image, picture.Bounds);
                                break;
                        }
                    }
                }
            }
        }
    }
    /// <summary>
    /// Data o jednom Image kresleném do DPicturePanel
    /// </summary>
    public class DPictureItem
    {
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="image"></param>
        public DPictureItem(Image image)
        {
            this.Image = image;
            this.Bounds = Rectangle.Empty;
            this.DrawMode = DPictureDrawMode.Default;
            this.Visible = false;
        }
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="image"></param>
        /// <param name="bounds"></param>
        public DPictureItem(Image image, Rectangle bounds)
        {
            this.Image = image;
            this.Bounds = bounds;
            this.DrawMode = DPictureDrawMode.Default;
            this.Visible = true;
        }
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="image"></param>
        /// <param name="bounds"></param>
        /// <param name="visible"></param>
        public DPictureItem(Image image, Rectangle bounds, bool visible)
        {
            this.Image = image;
            this.Bounds = bounds;
            this.DrawMode = DPictureDrawMode.Default;
            this.Visible = visible;
        }
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="image"></param>
        /// <param name="bounds"></param>
        /// <param name="drawMode"></param>
        public DPictureItem(Image image, Rectangle bounds, DPictureDrawMode drawMode)
        {
            this.Image = image;
            this.Bounds = bounds;
            this.DrawMode = drawMode;
            this.Visible = true;
        }
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="image"></param>
        /// <param name="bounds"></param>
        /// <param name="drawMode"></param>
        /// <param name="visible"></param>
        public DPictureItem(Image image, Rectangle bounds, DPictureDrawMode drawMode, bool visible)
        {
            this.Image = image;
            this.Bounds = bounds;
            this.DrawMode = drawMode;
            this.Visible = visible;
        }
        /// <summary>
        /// Kreslený obrázek
        /// </summary>
        public Image Image { get; set; }
        /// <summary>
        /// Souřadnice kreslení
        /// </summary>
        public Rectangle Bounds { get; set; }
        /// <summary>
        /// Režim kreslení
        /// </summary>
        public DPictureDrawMode DrawMode { get; set; }
        /// <summary>
        /// Viditelnost obrázku
        /// </summary>
        public bool Visible { get; set; }
        /// <summary>
        /// Libovolná data
        /// </summary>
        public object UserData;
    }
    public enum DPictureDrawMode
    {
        Default = 0,
        Unscaled,
        UnscaledAndClipped
    }
}
